let areas = ["北京市", "天津市", "河北省", "山西省", "内蒙古自治区", "辽宁省", "吉林省", "黑龙江省", "上海市", "江苏省", "浙江省", "安徽省", "福建省", "江西省", "山东省", "河南省", "湖北省", "湖南省", "广东省", "广西壮族自治区", "海南省", "重庆市", "四川省", "贵州省", "云南省", "西藏自治区", "陕西省", "甘肃省", "青海省", "宁夏回族自治区", "新疆维吾尔自治区", "台湾省", "香港特别行政区", "澳门特别行政区"]

module.exports = function(lab) {
    return {
        "1初始化用户": function() {
            let members = []
            for (var i = 0; i < 100; i++) {
                let acquire = []
                let consume = []
                let credit = 0
                for (var j = 0; j < Math.random() * 5; j++) {
                    let number = parseInt(Math.random() * 100) + 50
                    let activity_id = @.uuid()
                    let activity_name = `测试积分活动${j}`
                    acquire.push({
                        credit_acquired: number,
                        activity_id,
                        activity_name
                    })
                    if (Math.random() > 0.7) {
                        consume.push({
                            credit_consumed: number,
                            activity_id,
                            reason: "积分消耗"
                        })
                    } else {
                        credit += number
                    }
                }
                let area = areas[parseInt(areas.length * Math.random())]
                members.push({
                    member_no: i,
                    phone: `131${Math.random().toFixed(8).substr(2)}`,
                    type: 1,
                    gender: Math.random() > 0.5 ? "男" : "女",
                    name: `测试用户${i}`,
                    country: "中国",
                    province: area,
                    city: area,
                    district: area,
                    credit: credit,
                    creditEvents: {
                        acquire,
                        consume
                    }
                })
            }
            return @.async.all(members, function(form) {
                lab.slime.member.@importMember({ member: form, source: "wechat.connect" }).then(function(member) {
                    @.async(function() {
                        var temp = []
                        for (var i = 0; i < Math.random() * 5; i++) {
                            temp.push(i)
                        }
                        @.async.all(temp, function() {
                            lab.slime.member.@createEvent({
                                group: "member",
                                event: "visit",

                                member_id: member.id,
                                event_source: "slime",
                                source_event_id: "",
                                source_event_date: Date.now()
                            }).pipe(this)
                        }).pipe(this)
                    }).then(function() {
                        var temp = []
                        for (var i = 0; i < Math.random() * 5; i++) {
                            temp.push(i)
                        }
                        @.async.all(temp, function() {
                            lab.slime.member.@createEvent({
                                group: "member",
                                event: "purchase",

                                spending: parseInt(Math.random() * 500) + 200,

                                member_id: member.id,
                                event_source: "slime",
                                source_event_id: "",
                                source_event_date: Date.now()
                            }).pipe(this)
                        }).pipe(this)
                    }).then(function() {
                        var temp = []
                        for (var i = 0; i < Math.random() * 5; i++) {
                            temp.push(i)
                        }
                        @.async.all(temp, function(i) {
                            let coupon_no = @.uuid()
                            let coupon_id = @.uuid()
                            let coupon_name = `测试优惠活动${i}`
                            @.async(function() {
                                lab.slime.member.@createEvent({
                                    group: "coupon",
                                    event: "acquire",

                                    coupon_no,
                                    coupon_id,
                                    coupon_name,

                                    member_id: member.id,
                                    event_source: "slime",
                                    source_event_id: "",
                                    source_event_date: Date.now()
                                }).pipe(this)
                            }).then(function() {
                                if (Math.random() > 0.7) {
                                    lab.slime.member.@createEvent({
                                        group: "coupon",
                                        event: "consume",

                                        coupon_no,
                                        coupon_id,
                                        coupon_name,

                                        member_id: member.id,
                                        event_source: "slime",
                                        source_event_id: "",
                                        source_event_date: Date.now()
                                    }).pipe(this)
                                } else {
                                    this.next()
                                }
                            }).pipe(this)
                        }).pipe(this)
                    }).then(function() {
                        @.async.all(form.creditEvents.acquire, function({ credit_acquired, activity_id, activity_name }) {
                            @.async(function() {
                                lab.slime.member.@createEvent({
                                    group: "credit",
                                    event: "acquire",

                                    credit_acquired,
                                    activity_id,
                                    activity_name,

                                    member_id: member.id,
                                    event_source: "slime",
                                    source_event_id: "",
                                    source_event_date: Date.now()
                                }).pipe(this)
                            }).pipe(this)
                        }).pipe(this)
                    }).then(function() {
                        @.async.all(form.creditEvents.consume, function({ credit_consumed, activity_id, reason }) {
                            @.async(function() {
                                lab.slime.member.@createEvent({
                                    group: "credit",
                                    event: "consume",

                                    credit_consumed,
                                    activity_id,
                                    reason,

                                    member_id: member.id,
                                    event_source: "slime",
                                    source_event_id: "",
                                    source_event_date: Date.now()
                                }).pipe(this)
                            }).pipe(this)
                        }).pipe(this)
                    }).then(function() {
                        var temp = []
                        for (var i = 0; i < Math.random() * 5; i++) {
                            temp.push(i)
                        }
                        @.async.all(temp, function() {
                            lab.slime.member.@createEvent({
                                group: "weixinmp",
                                event: "message",

                                content: "测试留言",

                                member_id: member.id,
                                event_source: "slime",
                                source_event_id: "",
                                source_event_date: Date.now()
                            }).pipe(this)
                        }).pipe(this)
                    }).pipe(this)
                }).resolve().pipe(this);
            }).resolve()
        }
    }
}